package com.example.nfcschool;
import java.lang.reflect.Method;

import com.android.internal.telephony.ITelephony;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;


public class CallStateReceiver extends BroadcastReceiver {
//	Context context;

	
	@Override
	public void onReceive(Context context, Intent intent) {
	    ITelephony telephonyService = getTeleService(context);

	    if(telephonyService!=null) {
	        try {
	            String numberCall = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
	            
	                telephonyService.endCall();

	                Toast.makeText(context, "END CALL: " + numberCall, Toast.LENGTH_LONG).show();
	                Log.i("TAG", "END CALL");
	          
	        } catch (Exception e) {
	            e.printStackTrace();
	            Log.i("TAG", "CATCH CALL");
	        }
	    }
	}

	private ITelephony getTeleService(Context context) {
	    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
	    try {
	        Method m = Class.forName(tm.getClass().getName()).getDeclaredMethod("getITelephony");
	        m.setAccessible(true);
	        return (ITelephony) m.invoke(tm);
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    return null;
	}
	
		
}
